package com.niit.onlinemusicserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.onlinemusicserver.common.R;
import com.niit.onlinemusicserver.mapper.SongMapper;
import com.niit.onlinemusicserver.model.domain.Song;
import com.niit.onlinemusicserver.model.request.SongRequest;
import com.niit.onlinemusicserver.service.ObjectStoreService;
import com.niit.onlinemusicserver.service.SongService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {

  @Autowired
  private SongMapper songMapper;

  @Autowired
  private ObjectStoreService oss;
  @Override
  public R allSong() {
    return R.success(null, songMapper.selectList(null));
  }

  @Override
  public R addSong(SongRequest addSongRequest, MultipartFile mpfile) {
    Song song = new Song();
    BeanUtils.copyProperties(addSongRequest, song);
    String pic = "/img/songPic/tubiao.jpg";
    String fileName = System.currentTimeMillis() + "-" +mpfile.getOriginalFilename();
    String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";
    File file1 = new File(filePath);
    if (!file1.exists()) {
      if (!file1.mkdir()) {
        return R.fatal("创建文件失败");
      }
    }
    File dest = new File(filePath + System.getProperty("file.separator") + fileName);
    String storeUrlPath = "/song/" + fileName;
    try {
      mpfile.transferTo(dest);
    } catch (IOException e) {
      return R.fatal("上传失败" + e.getMessage());
    }
    song.setCreateTime(new Date());
    song.setUpdateTime(new Date());
    song.setPic(pic);
    song.setUrl(storeUrlPath);
    if (songMapper.insert(song) > 0) {
      return R.success("上传成功", storeUrlPath);
    } else {
      return R.error("上传失败");
    }
  }

  @Override
  public R updateSongMsg(SongRequest updateSongRequest) {
    Song song = new Song();
    BeanUtils.copyProperties(updateSongRequest, song);
    song.setUpdateTime(new Date());
    if (songMapper.updateById(song) > 0) {
      return R.success("修改成功");
    } else {
      return R.error("修改失败");
    }
  }

  @Override
  public R updateSongUrl(MultipartFile urlFile, int id) {
    String fileName = System.currentTimeMillis() + "-" +urlFile.getOriginalFilename();
    String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";
    File file1 = new File(filePath);
    if (!file1.exists()) {
      if (!file1.mkdir()) {
        return R.fatal("创建目的文件夹失败");
      }
    }
    File dest = new File(filePath + System.getProperty("file.separator") + fileName);
    String storeUrlPath = "/song/" + fileName;
    try {
      urlFile.transferTo(dest);
    } catch (IOException e) {
      return R.fatal("更新失败" + e.getMessage());
    }
    Song song = new Song();
    song.setId(id);
    song.setUrl(storeUrlPath);
    if (songMapper.updateById(song) > 0) {
      return R.success("更新成功", storeUrlPath);
    } else {
      return R.error("更新失败");
    }
  }

  @Override
  public R updateSongPic(MultipartFile urlFile, int id) {
    String fileName = System.currentTimeMillis() + "-" + urlFile.getOriginalFilename();
    String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + "songPic";
    File file1 = new File(filePath);
    if (!file1.exists()) {
      if (!file1.mkdir()) {
        return R.fatal("创建文件夹失败");
      }
    }

    File dest = new File(filePath + System.getProperty("file.separator") + fileName);
    String storeUrlPath = "/img/songPic/" + fileName;

    try {
      urlFile.transferTo(dest);
    } catch (IOException e) {
      return R.fatal("上传失败" + e.getMessage());
    }

//        String storeUrlPath = "img/songPic";
//        String ossPath;
//        try {
//            ossPath = oss.uploadFile(storeUrlPath, fileName, urlFile);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

    Song song = new Song();
    song.setId(id);
    //song.setPic(ossPath);
    song.setPic(storeUrlPath);
    song.setUpdateTime(new Date());
    if (songMapper.updateById(song) > 0) {
      return R.success("上传成功", storeUrlPath);
    } else {
      return R.error("上传失败");
    }
  }

  @Override
  public R deleteSong(Integer id) {
    if (songMapper.deleteById(id) > 0) {
      return R.success("删除成功");
    } else {
      return R.error("删除失败");
    }
  }

  @Override
  public R songOfSingerId(Integer singerId) {
    QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("singer_id",singerId);
    return R.success(null, songMapper.selectList(queryWrapper));
  }

  @Override
  public R songOfId(Integer id) {
    QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("id",id);
    return R.success(null, songMapper.selectList(queryWrapper));
  }

  @Override
  public R songOfSingerName(String name) {
    QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name",name);
    return R.success(null, songMapper.selectList(queryWrapper));
  }

  @Override
  public List<Integer> getAllSongIds() {
    return songMapper.selectAllSongIds();
  }

  @Override
  public R songOfSongListId(int songListId) {
    return R.success("查询成功", songMapper.selectSongListBySongListId(songListId));
  }

  @Override
  public R downloadSong(Integer id)  {
    String msg = "歌曲下载时发生错误";
    Song song = songMapper.selectById(id);
    if (song == null) {
      return R.error(msg);
    }

    String userHomeDir = System.getProperty("user.home");
    String songUrl = song.getUrl();
    String downloadDir = userHomeDir + "/OnlineMusic/Song/";
    oss.downloadFile(songUrl,downloadDir);

    return R.success("下载成功");
  }
}